//
//  UserAccount.swift
//  sxwb
//
//  Created by TianDu on 2016/11/16.
//  Copyright © 2016年 TianDu. All rights reserved.
//

import UIKit

class UserAccount: NSObject ,NSCoding{

    var access_token :String?
    var expires_in : TimeInterval = 0.0 {
        didSet{
            expires_date = Date(timeIntervalSinceNow: expires_in)
        }
    }
    var uid :String?
    var expires_date :Date?
    
    var avatar_large :String?
    var screen_name :String?
    
    
    /// 自定义构造函数
    init(_ dic:[String:Any]) {
        super.init()
        setValuesForKeys(dic)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
    }
    
    override var description: String{
        return dictionaryWithValues(forKeys: ["access_token","expires_date","uid","avatar_large","screen_name"]).description
    }
    
    
    // MARK:- 归档解档
    // 解档
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        expires_date = aDecoder.decodeObject(forKey: "expires_date") as? Date
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
        screen_name  = aDecoder.decodeObject(forKey: "screen_name") as? String
    }
    
    // 归档
    func encode(with aCoder: NSCoder) {
        
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(expires_date, forKey: "expires_date")
        aCoder.encode(avatar_large, forKey: "avatar_large")
        aCoder.encode(screen_name, forKey: "screen_name")
    }
}
